// Created by Roman Novitsky on 20.05.2009


#region Imported Namespaces


using System;
using System.Collections.Generic;
using System.Windows;

using WtfManager.EditingHistory;
using WtfManager.Framework;
using WtfManager.HierarchicalData;
using WtfManager.UIManager;


#endregion


namespace WtfManager.FileManager
{
  public class FileSystemItemDragDropHandlerExtension: Service, IDragDropHandlerExtension
  {
    #region Fields


    private EditingHistoryService _historyService;


    #endregion


    #region Properties/Indexers


    public override string Description
    {
      get
      {
        return "Handles drag and drop Drop events for File System items.";
      }
    }


    public Type DroppedItemType
    {
      get
      {
        return typeof(FileSystemItem);
      }
    }


    public Type DropTargetType
    {
      get
      {
        return typeof(FileSystemFolder);
      }
    }


    public override string Name
    {
      get
      {
        return "File System Item Drop Handler Extension";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
      if (State == FrameworkComponentState.Initialized)
      {
        _historyService = (EditingHistoryService)WtfFramework.Instance.Services[typeof(EditingHistoryService)];
      }
      else
      {
        _historyService = null;
      }
    }


    protected override void OnUnload()
    {
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    protected override FrameworkComponentState OnLoad()
    {
      AddDependency(typeof(EditingHistoryService));

      return FrameworkComponentState.Loaded;
    }


    public void DoDrop(object dropTarget, List<object> droppedItems, DragDropEffects effect)
    {
      if (State != FrameworkComponentState.Initialized)
      {
        return;
      }

      var folder = (FileSystemFolder)dropTarget;
      var changes = new List<Change>();

      if (effect == DragDropEffects.Move)
      {
        foreach (FileSystemItem droppedItem in droppedItems)
        {
          var change = new MoveChange(folder, droppedItem);
          changes.Add(change);
        }

        var changeSet = new ChangeSet("Move", "", changes);
        _historyService.Do(changeSet);
      }

      // TODO <RNovitsky, 21.05.2009>: Copy support
    }


    public DragDropEffects GetDropEffect(object dropTarget, List<object> droppedItems, DragDropKeyStates keyStates)
    {
      var target = (TreeItem)dropTarget;
      DragDropEffects effect = DragDropEffects.None;

      if (target is MyComputer)
      {
        return effect;
      }

      foreach (FileSystemItem droppedItem in droppedItems)
      {
        if (keyStates == DragDropKeyStates.ControlKey)
        {
          // TODO <RNovitsky, 21.05.2009>: Copy support
          effect = DragDropEffects.None;
        }
        else if (droppedItem.CanMoveTo(target))
        {
          effect = DragDropEffects.Move;
        }
      }

      return effect;
    }


    #endregion
  }
}